<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta
            name="viewport"
            content="initial-scale=1.0, user-scalable=no, width=device-width"
        />
        <style>
            html,
            body,
            #container {
                width: 100%;
                height: 100%;
            }
            #panel {
                position: fixed;
                background-color: white;
                max-height: 90%;
                overflow-y: auto;
                top: 10px;
                right: 10px;
                width: 280px;
            }
            #panel .amap-call {
                background-color: #009cf9;
                border-top-left-radius: 4px;
                border-top-right-radius: 4px;
            }
            #panel .amap-lib-driving {
                border-bottom-left-radius: 4px;
                border-bottom-right-radius: 4px;
                overflow: hidden;
            }
        </style>
        <title>位置经纬度 + 获取驾车规划数据</title>
        <link
            rel="stylesheet"
            href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"
        />
        <script type="text/javascript">
            window._AMapSecurityConfig = {
                securityJsCode: '7b1debe426ef12adff1dd0cd592c0405',
            }
        </script>
        <script src="https://webapi.amap.com/maps?v=2.0&key=46f0497e72ff7dddef9088430a7f7012&plugin=AMap.Driving"></script>
        <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
    </head>
    <body>
        出发点：<input
            type="text"
            id="sA"
            onblur="getStart()"
            value="113.375682,34.787544"
        />
        终点：<input
            type="text"
            id="eA"
            onblur="getEnd()"
            value="113.823014,34.193251"
        />
        <button onclick="startMap()">开始导航</button>
        <div id="container"></div>
        <div id="panel"></div>

        <script type="text/javascript">
            var startAddress = null,
                endAddress = null
            function getStart() {
                startAddress = sA.value.split(',')
                console.log(startAddress)
            }
            function getEnd() {
                endAddress = eA.value.split(',')
                console.log(endAddress)
            }
            function startMap() {
                // 根据起终点经纬度规划驾车导航路线
                driving.search(
                    new AMap.LngLat(startAddress[0], startAddress[1]),
                    new AMap.LngLat(endAddress[0], endAddress[1]),
                    function (status, result) {
                        // result 即是对应的驾车导航信息，相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
                        if (status === 'complete') {
                            log.success('绘制驾车路线完成')
                        } else {
                            log.error('获取驾车数据失败：' + result)
                        }
                    }
                )
            }
            var map = new AMap.Map('container', {
                // center: [116.397559, 39.89621],
                zoom: 14,
            })

            var drivingOption = {
                policy: AMap.DrivingPolicy.LEAST_TIME, // 其它policy参数请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingPolicy
                ferry: 1, // 是否可以使用轮渡
                province: '京', // 车牌省份的汉字缩写
                map: map,
                panel: 'panel',
            }

            // 构造路线导航类
            var driving = new AMap.Driving(drivingOption)
        </script>
    </body>
</html>
